package com.yupi.yuaiagent.rag;

import com.yupi.yuaiagent.splitter.MyTokenTextSplitter;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Configuration
public class LoveAppVectorStoreConfig {

    @Resource
    private LoveAppDocumentLoader loveAppDocumentLoader;
    @Resource
    private MyTokenTextSplitter myTokenTextSplitter;
    @Resource
    private MyKeywordEnricher myKeywordEnricher;


    @Bean
    VectorStore loveAppVectorStore(EmbeddingModel dashscopeEmbeddingModel) throws IOException {
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(dashscopeEmbeddingModel)
                .build();


        // 自己构造相似度搜索请求
//        SearchRequest request = SearchRequest.builder()
//                .query("如何去维持日常恋爱关系？")   // 查询提示词
//                .topK(5)                  // 返回最相似的5个结果
//                .similarityThreshold(0.3) // 相似度阈值，0.0-1.0之间   分数要超过0.3才算相似
//                .filterExpression("category == 'web' AND date > '2025-05-03'")  // 过滤表达式
//                .filterExpression("status == '恋爱' AND title == '如何在恋爱中与对方有效'")  // 过滤元信息表达式 精确匹配
//                .build();
        // 加载文档
        List<Document> documents = loveAppDocumentLoader.loadMarkdowns();

        // 自主切分
//        List<Document> splitDocuments = myTokenTextSplitter.splitCustomized(documents);
//        List<Document> enrichDocuments = myKeywordEnricher.enrichDocuments(documents);
        simpleVectorStore.add(documents);
//        List<Document> similarResult = simpleVectorStore.similaritySearch(request);
        return simpleVectorStore;
    }
}
